package com.bocloud.cmp.service.thread;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.dao.AlarmDao;
import com.bocloud.cmp.driver.providers.intf.AlarmProvider;
import com.bocloud.cmp.entity.resource.Alarm;
import com.bocloud.cmp.entity.resource.CloudVendor;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.ListTool;

/**
 * 告警同步任务类
 * 
 * @author dmw
 *
 */
public class AlarmSyncTask {
	private static Logger logger = LoggerFactory.getLogger(AlarmSyncTask.class);
	private Long userId;// 操作者ID
	private CloudVendor vendor; // 云平台
	private AlarmDao alarmDao;// 告警数据库操作接口
	private AlarmProvider provider;// 告警提供者

	public void execute() {
		// 启动一个线程
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					logger.info("Start to Sync alarm from cloudvendor【{}】", vendor.getName());
					alarmDao.deleteByVendorId(vendor.getId());
					logger.info("physical delete all old alarms from database");
					BsmResult result = provider.list(vendor.getUuid(), null);
					if (null == result.getData()) {
						logger.info("No alarms get from cloudvendor {}", vendor.getName());
						return;
					}
					String content = JSONObject.toJSONString(result.getData());
					List<Alarm> newAlarms = JSONObject.parseArray(content, Alarm.class);
					if (ListTool.isEmpty(newAlarms)) {
						logger.info("No alarms get from cloudvendor {}", vendor.getName());
						return;
					}
					for (Alarm alarm : newAlarms) {
						alarm.setGmtCreate(new Date());
						alarm.setGmtModify(new Date());
						alarm.setMenderId(userId);
						alarm.setCreaterId(userId);
						alarm.setVendorId(vendor.getId());
						if (null == alarm.getConfirmer()) {
							alarm.setStatus("UNCONFIRM");
						} else {
							alarm.setStatus("CONFIRMED");
						}
						if (VendorType.VMWARE.name().equals(vendor.getType())) {
							if (alarm.getLevel().equals("red")) {
								alarm.setLevel("DANGER");
							} else {
								alarm.setLevel("WARN");
							}
						}
					}
					logger.info("new alarm size from cloudvendor [{}] is {}", vendor.getName(), newAlarms.size());
					alarmDao.batchSave(newAlarms, 1000);
				} catch (Exception e) {
					logger.error("Sync cloudvendor {} alarm error !", vendor.getName(), e);
				}
			}
		}).start();
	}

	public AlarmSyncTask(Long userId, CloudVendor vendor, AlarmDao alarmDao, AlarmProvider provider) {
		super();
		this.userId = userId;
		this.vendor = vendor;
		this.alarmDao = alarmDao;
		this.provider = provider;
	}

	/**
	 * @return the userId
	 */
	public Long getUserId() {
		return userId;
	}

	/**
	 * @param userId
	 *            the userId to set
	 */
	public void setUserId(Long userId) {
		this.userId = userId;
	}

	/**
	 * @return the vendor
	 */
	public CloudVendor getVendor() {
		return vendor;
	}

	/**
	 * @param vendor
	 *            the vendor to set
	 */
	public void setVendor(CloudVendor vendor) {
		this.vendor = vendor;
	}

	/**
	 * @return the alarmDao
	 */
	public AlarmDao getAlarmDao() {
		return alarmDao;
	}

	/**
	 * @param alarmDao
	 *            the alarmDao to set
	 */
	public void setAlarmDao(AlarmDao alarmDao) {
		this.alarmDao = alarmDao;
	}

	/**
	 * @return the provider
	 */
	public AlarmProvider getProvider() {
		return provider;
	}

	/**
	 * @param provider
	 *            the provider to set
	 */
	public void setProvider(AlarmProvider provider) {
		this.provider = provider;
	}

}
